package jungletimers;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author nmaludy
 */
public class CreepTimerPanel extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private static final NumberFormat MINUTE_FORMAT = NumberFormat.getInstance();
    private static final NumberFormat SECOND_FORMAT = NumberFormat.getNumberInstance();
    private static final Border DEFAULT_BORDER = (new JLabel()).getBorder();
    private static final Border FINISHED_BORDER = BorderFactory.createLineBorder(Color.RED, 2);

    static {
        // pad seconds with a leading 0 if they have less than 2 digits
        SECOND_FORMAT.setMinimumIntegerDigits(2);
    }
    
    private final JLabel creepLabel;

    private final JLabel timerLabel;

    private final long respawnInMilliseconds;
    
    //The number of milliseconds left in the timer. This value is decremented periodically.
    private long remaining;

    //The last time that this timer was updated
    private long lastUpdate;
    
    public CreepTimerPanel(String creepName, int respawnInMinutes) {
        super();
        this.respawnInMilliseconds = (respawnInMinutes * 60 * 1000);
        this.remaining = respawnInMilliseconds;
        this.creepLabel = new JLabel(creepName + ": ");
        this.timerLabel = new JLabel();
        updateTimerLabel();
        layoutUi();
    }

    private void layoutUi() {
        MigLayout ml = new MigLayout("insets 0 0 0 0", //Layout constraints
                "[fill][fill]", //column constraints
                "[fill]"); //row constraints
        super.setLayout(ml);
        super.add(creepLabel, "grow");
        super.add(timerLabel, "grow");
    }

    private void update() {
        long now = System.currentTimeMillis(); // current time in ms
        if (lastUpdate == 0) {
            lastUpdate = now;
        }
        long elapsed = now - lastUpdate; // ms elapsed since last update
        remaining -= elapsed; // adjust remaining time
        lastUpdate = now; // remember this update time

        // Convert remaining milliseconds to mm:ss format and display
        if (remaining < 0) {
            remaining = 0;
        }
        updateTimerLabel();

        //Completion
        if (remaining == 0) {
            super.setBorder(FINISHED_BORDER);
        }
    }

    private void updateTimerLabel() {
        long minutes = (remaining / 60000);
        long seconds = ((remaining % 60000) / 1000);
        timerLabel.setText(MINUTE_FORMAT.format(minutes) + ":" + SECOND_FORMAT.format(seconds));
    }

    public void actionPerformed(ActionEvent e) {
        update();
    }

    public void reset() {
        super.setBorder(DEFAULT_BORDER);
        remaining = respawnInMilliseconds;
        lastUpdate = 0;
        updateTimerLabel();
    }
}
